package com.android.fragmentdemo.definervlayoutmanager;
import android.util.Log;
import android.view.View;
import androidx.recyclerview.widget.RecyclerView;

public class CustomLayoutManager3 extends RecyclerView.LayoutManager {
    private static final String  TAG = "CustomLayoutManager3";
    private int mSumDy = 0;
    private int mTotalHeight = 0;
    private int mItemHeight = 0;

    @Override
    public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
        initialItem(recycler);
        removeAllViews();
        detachAndScrapAttachedViews(recycler);
        layoutItems(recycler);
        //如果所有子view的高度和没有填满RecyclerView,则将高度设置为RecyclerView高度
        mTotalHeight = Math.max(mItemHeight * getItemCount(), getVerticalSpace());
    }

    @Override
    public boolean canScrollVertically() {
        return true;
    }

    @Override
    public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
        detachAndScrapAttachedViews(recycler);
        mSumDy += dy;
        if (mSumDy < 0) {
            //滑动到最顶部
            mSumDy = 0;
        } else if (mSumDy > mTotalHeight - getVerticalSpace()) {
            //滑动底部
            mSumDy = mTotalHeight - getVerticalSpace();
        }
        //布局item
        layoutItems(recycler);
        //回收item
        recycleItems(recycler, dy);
        return dy;
    }

    @Override
    public RecyclerView.LayoutParams generateDefaultLayoutParams() {
        return new RecyclerView.LayoutParams(RecyclerView.LayoutParams.WRAP_CONTENT, RecyclerView.LayoutParams.WRAP_CONTENT);
    }

    private int getVerticalSpace() {
        return getHeight() - getPaddingTop() - getPaddingBottom();
    }

    //初始化item
    private void initialItem(RecyclerView.Recycler recycler) {
        View view = recycler.getViewForPosition(0);
        addView(view);
        measureChildWithMargins(view, 0, 0);
        mItemHeight = getDecoratedMeasuredHeight(view);
        removeAndRecycleView(view, recycler);
    }

    //回收item
    private void recycleItems(RecyclerView.Recycler recycler, int dy) {
        int itemCount = getChildCount();
        for (int i = 0; i < itemCount; i++) {
            View view = getChildAt(i);
            if (view == null) {
                continue;
            }
            if (dy > 0) {
                if (view.getBottom() < 0) {
                    removeAndRecycleView(view, recycler);
                }
            } else {
                if (view.getTop() > getHeight()) {
                    removeAndRecycleView(view, recycler);
                }
            }
        }
    }

    private void layoutItems(RecyclerView.Recycler recycler) {
        int offsetY = 0;
        for (int i = 0; i < getItemCount(); i++) {
            View child = recycler.getViewForPosition(i);
            child.setRotationX(5);
            addView(child);
            measureChildWithMargins(child, 0, 0);
            int mWidth = getDecoratedMeasuredWidth(child);
            int mHeight = getDecoratedMeasuredHeight(child);
            layoutDecoratedWithMargins(child, 0, offsetY - mSumDy, mWidth, offsetY - mSumDy + mHeight);
            offsetY += mHeight;
            if (offsetY - mSumDy >= getVerticalSpace()) {
                break;
            }
        }
    }
}
